package com.example.android.commitcontent.ime.cores.utils;

import android.content.Context;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.android.commitcontent.ime.R;
import com.example.android.commitcontent.ime.cores.adapters.BaseRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuyougen
 * 2019.06.14
 * create a PopupWindow dialog like the PopupMenu's effect ,
 * you can use it to generate a menu dialog fast.
 */
public class PopupHelper {

    private Context mContext;
    private PopupWindow mPopupWindow;
    private OnItemClickListener mOnItemClickListener;
    private String mTitleText;
    private float mDensity;
    private int mPopupWidth;
    private List<String> titleList = new ArrayList<>();

    private PopupHelper(Context context) {
        this.mContext = context;
        mDensity = context.getResources().getDisplayMetrics().density;
        mPopupWidth = (int) (mDensity * 190);
    }

    public static PopupHelper newInstance(Context context) {
        return new PopupHelper(context);
    }

    public PopupHelper setTitleText(String titleText) {
        this.mTitleText = titleText;
        return this;
    }

    public PopupHelper setPopupWidth(int width, boolean isDip) {
        this.mPopupWidth = width;
        if (isDip)
            this.mPopupWidth = (int) (mDensity * width);
        return this;
    }

    @SuppressWarnings("unchecked")
    public <T> PopupHelper setDataList(List<T> list, DataConverter<T> converter) {
        if (list.size() == 0) return this;
        if (list.get(0) instanceof String) {
            titleList.addAll((List<String>) list);
            return this;
        }
        if (converter == null)
            throw new RuntimeException("the converter must not the null.");
        for (T t : list)
            titleList.add(converter.convertToString(t));
        return this;
    }

    public void showAsDropDown(View view, int offsetX, int offsetY, boolean isDip) {
        if (isDip) {
            offsetX = (int) (offsetX * mDensity);
            offsetY = (int) (offsetY * mDensity);
        }
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        if (!TextUtils.isEmpty(mTitleText))
            linearLayout.addView(createTextView(),
                    new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                            , ViewGroup.LayoutParams.WRAP_CONTENT)
            );
        linearLayout.addView(createRecyclerView(),
                new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
        );
        mPopupWindow = new PopupWindow(linearLayout, mPopupWidth, ViewGroup.LayoutParams.WRAP_CONTENT, true);
        int padding = (int) (6 * mDensity);
        linearLayout.setPadding(0, padding, 0, padding);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mPopupWindow.setElevation(mDensity * 8);
        }
        mPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(mContext, R.drawable.bg_popup));
        mPopupWindow.showAsDropDown(view, offsetX, offsetY);
    }

    private RecyclerView createRecyclerView() {
        RecyclerView recyclerView = new RecyclerView(mContext);
        recyclerView.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, R.anim.layout_anim_bottom_fly_in));
        recyclerView.setAdapter(new BaseRecyclerViewAdapter<String>(mContext, R.layout.item_popup_list, titleList) {
            @Override
            public void onHolderCreated(final BaseRecyclerViewAdapter.Holder holder) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (mOnItemClickListener != null) {
                            mOnItemClickListener.onItemClick(mPopupWindow, holder.getAdapterPosition());
                        }
                    }
                });
            }

            @Override
            protected void onBindData(@NonNull String data, @NonNull Holder holder, int position) {
                holder.text(R.id.tv_title, data);
            }
        });
        recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        return recyclerView;
    }

    private TextView createTextView() {
        TextView textView = new TextView(mContext);
        textView.setTextSize(13f);
        textView.setText(mTitleText);
        int horizontalPadding = (int) (mDensity * 14);
        int verticalPadding = (int) (mDensity * 10);
        textView.setPadding(horizontalPadding, verticalPadding, horizontalPadding, verticalPadding / 2);
        return textView;
    }

    public PopupHelper setItemListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
        return this;
    }

    public interface DataConverter<T> {
        String convertToString(T data);
    }

    public interface OnItemClickListener {
        void onItemClick(PopupWindow popupWindow, int position);
    }
}
